/*
 * Copyright (C) 2018 Logos Technology Ltd.
 */

package com.logos.tool;

import android.content.Context;

/**
 * Created by hk on 2018/8/22.
 */
public class ResourceUtil {

    /**
     * getId
     *
     * @param context      context
     * @param resourceName resourceName
     * @return id
     */
    public static int getId(Context context, String resourceName) {
        return getIdentifierByType(context, resourceName, "id");
    }

    /**
     * getId
     *
     * @param context      context
     * @param resourceName resourceName
     * @return id
     */
    public static int getLayoutId(Context context, String resourceName) {
        return getIdentifierByType(context, resourceName, "layout");
    }

    /**
     * getId
     *
     * @param context      context
     * @param resourceName resourceName
     * @return id
     */
    public static int getStringId(Context context, String resourceName) {
        return getIdentifierByType(context, resourceName, "string");
    }

    /**
     * getId
     *
     * @param context      context
     * @param resourceName resourceName
     * @return id
     */
    public static int getDrawableId(Context context, String resourceName) {
        return getIdentifierByType(context, resourceName, "drawable");
    }

    /**
     * getId
     *
     * @param context      context
     * @param resourceName resourceName
     * @return id
     */
    public static int getMipmapId(Context context, String resourceName) {
        return getIdentifierByType(context, resourceName, "mipmap");
    }

    /**
     * getId
     *
     * @param context      context
     * @param resourceName resourceName
     * @return id
     */
    public static int getColorId(Context context, String resourceName) {
        return getIdentifierByType(context, resourceName, "color");
    }

    /**
     * getId
     *
     * @param context      context
     * @param resourceName resourceName
     * @return id
     */
    public static int getColor(Context context, String resourceName) {
        try {
            return context.getResources().getColor(getColorId(context, resourceName));
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * getId
     *
     * @param context      context
     * @param resourceName resourceName
     * @return id
     */
    public static int getDimenId(Context context, String resourceName) {
        return getIdentifierByType(context, resourceName, "dimen");
    }

    /**
     * getId
     *
     * @param context      context
     * @param resourceName resourceName
     * @return id
     */
    public static float getDimension(Context context, String resourceName) {
        try {
            return context.getResources().getDimension(getDimenId(context, resourceName));
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * getId
     *
     * @param context      context
     * @param resourceName resourceName
     * @return id
     */
    public static int getAttrId(Context context, String resourceName) {
        return getIdentifierByType(context, resourceName, "attr");
    }

    /**
     * getId
     *
     * @param context      context
     * @param resourceName resourceName
     * @return id
     */
    public static int getStyleId(Context context, String resourceName) {
        return getIdentifierByType(context, resourceName, "style");
    }

    /**
     * getId
     *
     * @param context      context
     * @param resourceName resourceName
     * @return id
     */
    public static int getAnimId(Context context, String resourceName) {
        return getIdentifierByType(context, resourceName, "anim");
    }

    /**
     * getId
     *
     * @param context      context
     * @param resourceName resourceName
     * @return id
     */
    public static int getArrayId(Context context, String resourceName) {
        return getIdentifierByType(context, resourceName, "array");
    }

    /**
     * getId
     *
     * @param context      context
     * @param resourceName resourceName
     * @return id
     */
    public static int getIntegerId(Context context, String resourceName) {
        return getIdentifierByType(context, resourceName, "integer");
    }

    /**
     * getId
     *
     * @param context      context
     * @param resourceName resourceName
     * @return id
     */
    public static int getBoolId(Context context, String resourceName) {
        return getIdentifierByType(context, resourceName, "bool");
    }

    private static int getIdentifierByType(Context context, String resourceName, String defType) {
        try {
            return context.getResources().getIdentifier(resourceName,
                    defType,
                    context.getPackageName());
        } catch (Exception e) {
            return 0;
        }
    }
}
